package com.foruo.demo.redis.pubsub;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

/**
 * 订阅者
 * @author GaoYuan
 * @date 2018/9/17 下午9:06
 */
public class Subscriber extends JedisPubSub{


    @Override
    public void onMessage(String channel, String message) {
        System.out.println("收到频道【" + channel + "】的消息：" + message);
    }

    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println("成功订阅【"+ channel +"】");
    }

    public static void main(String[] args){
        Jedis jedis = null;
        try {
            jedis = new Jedis(JedisConfig.HOST, JedisConfig.PORT);
            jedis.subscribe(new Subscriber(), "channel1");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
